package com.lavida.project.dashboard.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lavida.common.utils.BigDecimalUtil;
import com.lavida.common.utils.security.ShiroUtils;
import com.lavida.framework.web.controller.BaseController;
import com.lavida.project.dashboard.domain.PieData;
import com.lavida.project.system.attendance.mapper.AttendanceMapper;
import com.lavida.project.system.dept.domain.Dept;
import com.lavida.project.system.dept.mapper.DeptMapper;
import com.lavida.project.system.emp.mapper.EmpMapper;
import com.lavida.project.system.emp.mapper.RosterMapper;
import com.lavida.project.system.goods.domain.Goods;
import com.lavida.project.system.goods.mapper.GoodsMapper;
import com.lavida.project.system.goods.mapper.SubGoodsMapper;
import com.lavida.project.system.user.domain.User;
import com.lavida.project.system.user.service.IUserService;

/**
 * 用户信息
 * 
 */
@Controller
@RequestMapping("/dashboard")
public class DashboradController extends BaseController
{
    private String prefix = "dashboard";
    
    @Autowired
    private RosterMapper rosterMapper;
    
    @Autowired
    private EmpMapper empMapper;
    
    @Autowired
    private DeptMapper deptMapper;
    
    @Autowired
    private GoodsMapper goodsMapper;
    
    @Autowired
    private SubGoodsMapper subGoodsMapper;
    
    @Autowired
    private AttendanceMapper attendaceMapper;
    
    @Autowired
	private IUserService userService;
    @GetMapping()
	public String setting(ModelMap mmap) {
		User user  = ShiroUtils.getSysUser();
		User user1 = userService.selectUserById(user.getUserId());
		mmap.put("user", user1);
		return "index";
	}
    /**
     * 本月、环比：入职人数，转正人数，调岗人数，离职人数
     * @param mmap
     * @return
     */
    //@RequiresPermissions("dashboard:empindicator:view")
    @RequestMapping("/empindicator")
    @ResponseBody
    public Map<String,Object> empIndicator(ModelMap mmap)
    {
    	Map<String,Object> result = new HashMap<String,Object>();
    	//本月在职人数
    	Integer curHireEmpNums = rosterMapper.selectIntervalMonthEmpNums("hiredate", 0);
    	//上个月在职人数
    	Integer preHireEmpNums = rosterMapper.selectIntervalMonthEmpNums("hiredate", 1);
    	//在职人数环比增长率
    	String hireGrowthRateStr = "-";
    	if(preHireEmpNums!=0&&preHireEmpNums!=null) {
    		Double hireGrowthRate = BigDecimalUtil.div((curHireEmpNums - preHireEmpNums), preHireEmpNums, 4).doubleValue();
    		hireGrowthRateStr = hireGrowthRate * 100 + "%";
    	}
    	//本月离职人数
    	Integer curLeaveEmpNums = rosterMapper.selectIntervalMonthEmpNums("leavetime", 0);
    	//上个月离职人数
    	Integer preLeaveEmpNums = rosterMapper.selectIntervalMonthEmpNums("leavetime", 1);
    	//离职人数环比增长率
    	String leaveGrowthRateStr = "-";
    	if(preHireEmpNums!=0&&preHireEmpNums!=null) {
    		Double leaveGrowthRate = BigDecimalUtil.div((curLeaveEmpNums - preLeaveEmpNums), preLeaveEmpNums, 4).doubleValue();
    		leaveGrowthRateStr = leaveGrowthRate * 100 + "%";
    	}
    	
    	//本月转正人数
    	Integer curConfirmEmpNums = empMapper.selectIntervalMonthEmpNums("empconfirmation", 0);
    	//上个月转正人数
    	Integer preConfirmEmpNums = empMapper.selectIntervalMonthEmpNums("empconfirmation", 1);
    	//转正人数环比增长率
    	String confirmGrowthRateStr = "-";
    	if(preConfirmEmpNums!=0&&preConfirmEmpNums!=null) {
    		Double confirmGrowthRate = BigDecimalUtil.div((curConfirmEmpNums - preConfirmEmpNums), preConfirmEmpNums, 4).doubleValue();
    		confirmGrowthRateStr = confirmGrowthRate * 100 + "%";
    	}
    	
    	result.put("curHireEmpNums",curHireEmpNums);
    	result.put("preHireEmpNums",preHireEmpNums);
    	result.put("hireGrowthRateStr",hireGrowthRateStr);
    	result.put("curLeaveEmpNums",curLeaveEmpNums);
    	result.put("preLeaveEmpNums",preLeaveEmpNums);
    	result.put("leaveGrowthRateStr",leaveGrowthRateStr);
    	result.put("curConfirmEmpNums",curConfirmEmpNums);
    	result.put("preConfirmEmpNums",preConfirmEmpNums);
    	result.put("confirmGrowthRateStr",confirmGrowthRateStr);
    	
    	
    	return result;
    }
    /**
     * 获取本年度各月在职人数
     * @param mmap
     * @return
     */
    @RequestMapping("/workemp")
    @ResponseBody
    public Map<String,Object> workEmp(ModelMap mmap)
    {
    	Map<String,Object> result = new HashMap<String,Object>();
    	String[] months = new String[] {"01","02","03","04","05","06","07","08","09","10","11","12"};
    	List<Integer> beginWorkEmpArray = new ArrayList<Integer>();
    	List<Integer> middleWorkEmpArray = new ArrayList<Integer>();
    	List<Integer> endWorkEmpArray = new ArrayList<Integer>();
    	for(int i=0;i<months.length;i++) {
    		//月初在职人数
    		Integer mbeginWorkEmpNums = rosterMapper.selectBeginMonthWorkEmpNums(months[i]);
    		//月末在职人数
    		Integer mEndWorkEmpNums = rosterMapper.selectEndMonthWorkEmpNums(months[i]);
    		System.out.println(months[i]+","+mEndWorkEmpNums);
    		//月中在职人数
    		Integer mMiddelWorkEmpNums = (mbeginWorkEmpNums+mEndWorkEmpNums)/2;
    		beginWorkEmpArray.add(mbeginWorkEmpNums);
    		middleWorkEmpArray.add(mMiddelWorkEmpNums);
    		endWorkEmpArray.add(mEndWorkEmpNums);
    	}
    	result.put("beginWorkEmpArray", beginWorkEmpArray);
    	result.put("middleWorkEmpArray", middleWorkEmpArray);
    	result.put("endWorkEmpArray", endWorkEmpArray);
    	return result;
    }
    
    /**
     * 获取各个部门成本
     * @param mmap
     * @return
     */
    @RequestMapping("/departmentcost")
    @ResponseBody
    public Map<String,Object> departmentCost(ModelMap mmap)
    {
    	Map<String,Object> result = new HashMap<String,Object>();
    	//获取部门
    	List<Dept> nodeDept = deptMapper.selectNodeDept(2);
    	List<String> xAxisData = new ArrayList<String>();
    	for(Dept dept:nodeDept) {
    		xAxisData.add(dept.getParentName()+"-"+dept.getDeptName());
    	}
    	result.put("xAxisData", xAxisData);
    	//办公用品
    	List<Goods> allGoods = goodsMapper.selectAllGoods();
    	List<String> legendData = new ArrayList<String>();
    	for(Goods goods:allGoods) {
    		legendData.add(goods.getGoods_name());
    	}
    	result.put("legendData", legendData);
    	//计算部门成本
    	List<List<Double>> goodsList = new ArrayList<List<Double>>();
		for(Goods goods:allGoods) {
			//所有部门的一个产品
			List<Double> goodsOne = new ArrayList<Double>();
			for(Dept dept:nodeDept) {
				//领用数量
				Integer ly_amount = subGoodsMapper.selectOperateAmount(dept.getParentName(), dept.getDeptName(), "领用", goods.getId());
				//归还数量
				Integer gh_amount = subGoodsMapper.selectOperateAmount(dept.getParentName(), dept.getDeptName(), "归还", goods.getId());
				if(ly_amount==null) {
					ly_amount = 0;
				}
				if(gh_amount==null) {
					gh_amount = 0;
				}
				//部门成本 = (领用数量-归还数量)*价格
				double departmentCost = BigDecimalUtil.mul((ly_amount-gh_amount), goods.getPrice()).doubleValue();
				goodsOne.add(departmentCost);
			}
			goodsList.add(goodsOne);
    	}
		result.put("goodsList", goodsList);
    	return result;
    }
    
    /**
     * 获取在职人员结构
     * @param mmap
     * @return
     */
    @RequestMapping("/workempstruct")
    @ResponseBody
    public Map<String,Object> workEmpStruct(ModelMap mmap)
    {
    	Map<String,Object> result = new HashMap<String,Object>();
    	//学历结构
    	String[] eduArray = new String[] {"中专","大专","本科","硕士","博士"};
    	List<PieData> eduEmpNumList = new ArrayList<PieData>();
    	for(int i=0;i<eduArray.length;i++) {
    		PieData pieData = new PieData();
    		Integer eduEmpNum = empMapper.getCurrentEducationEmpNum(eduArray[i]);
    		pieData.setName(eduArray[i]);
    		pieData.setValue(eduEmpNum);
    		eduEmpNumList.add(pieData);
    	}
    	result.put("eduEmpNumList", eduEmpNumList);
    	//年龄结构
    	//String[] ageArray = new String[] {"小于等于25","大于25且小于等于30","大于30且小于等于45","大于45"};
    	List<PieData> ageEmpNumList = new ArrayList<PieData>();
    	Integer ageLT25EmpNum = empMapper.getCurrentAgeLTEqual25EmpNum();
    	Integer ageLT30EmpNum = empMapper.getCurrentAgeLTEqual30EmpNum();
    	Integer ageLT45EmpNum = empMapper.getCurrentAgeLTEqual45EmpNum();
    	Integer ageGT45EmpNum = empMapper.getCurrentAgeGT45EmpNum();
		PieData pieData2_1 = new PieData();
		pieData2_1.setName("小于等于25");
		pieData2_1.setValue(ageLT25EmpNum);
		ageEmpNumList.add(pieData2_1);
		PieData pieData2_2 = new PieData();
		pieData2_2.setName("大于25且小于等于30");
		pieData2_2.setValue(ageLT30EmpNum);
		ageEmpNumList.add(pieData2_2);
		PieData pieData2_3 = new PieData();
		pieData2_3.setName("大于30且小于等于45");
		pieData2_3.setValue(ageLT45EmpNum);
		ageEmpNumList.add(pieData2_3);
		PieData pieData2_4 = new PieData();
		pieData2_4.setName("大于45");
		pieData2_4.setValue(ageGT45EmpNum);
		ageEmpNumList.add(pieData2_4);
		result.put("ageEmpNumList", ageEmpNumList);
		//性别结构
		List<PieData> sexEmpNumList = new ArrayList<PieData>();
		Integer maleEmpNum = empMapper.getCurrentSexEmpNum("男");
		Integer femaleEmpNum = empMapper.getCurrentSexEmpNum("女");
		PieData pieData3_1 = new PieData();
		pieData3_1.setName("男");
		pieData3_1.setValue(maleEmpNum);
		sexEmpNumList.add(pieData3_1);
		PieData pieData3_2 = new PieData();
		pieData3_2.setName("女");
		pieData3_2.setValue(femaleEmpNum);
		sexEmpNumList.add(pieData3_2);
		result.put("sexEmpNumList", sexEmpNumList);
		//国籍结构
		List<PieData> nationEmpNumList = new ArrayList<PieData>();
		Integer chinaEmpNum = empMapper.getCurrentNationEmpNum("中国");
		Integer notChinaEmpNum = empMapper.getCurrentNotNationEmpNum("中国");
		PieData pieData4_1 = new PieData();
		pieData4_1.setName("中国籍");
		pieData4_1.setValue(chinaEmpNum);
		nationEmpNumList.add(pieData4_1);
		PieData pieData4_2 = new PieData();
		pieData4_2.setName("外国籍");
		pieData4_2.setValue(notChinaEmpNum);
		nationEmpNumList.add(pieData4_2);
		result.put("nationEmpNumList", nationEmpNumList);
    	return result;
    }
    
    /**
     * 获取本年度各月考勤分析
     * @param mmap
     * @return
     */
    @RequestMapping("/attendanceanalysis")
    @ResponseBody
    public Map<String,Object> attendanceAnalysis(ModelMap mmap)
    {
    	Map<String,Object> result = new HashMap<String,Object>();
    	String[] months = new String[] {"01","02","03","04","05","06","07","08","09","10","11","12"};
    	List<Double> sumWorkTimeArray = new ArrayList<Double>();
    	List<Double> sumLeaveTimeArray = new ArrayList<Double>();
    	List<Double> sumOverTimeArray = new ArrayList<Double>();
    	for(int i=0;i<months.length;i++) {
    		//工时
    		Double sumWorkTime = attendaceMapper.getSumWorkTime(months[i]);
    		if(sumWorkTime==null) {
    			sumWorkTime = 0.0;
    		}
    		//请假工时
    		Double sumLeaveTime = attendaceMapper.getSumLeaveTime(months[i]);
    		if(sumLeaveTime==null) {
    			sumLeaveTime = 0.0;
    		}
    		//加班工时
    		Double sumOverTime = attendaceMapper.getSumOverTime(months[i]);
    		if(sumOverTime==null) {
    			sumOverTime = 0.0;
    		}
    		sumWorkTimeArray.add(sumWorkTime);
    		sumLeaveTimeArray.add(sumLeaveTime);
    		sumOverTimeArray.add(sumOverTime);
    	}
    	result.put("sumWorkTimeArray", sumWorkTimeArray);
    	result.put("sumLeaveTimeArray", sumLeaveTimeArray);
    	result.put("sumOverTimeArray", sumOverTimeArray);
    	return result;
    }
    
    
    
    
}